// pages/material/selectCoupon/selectCoupon.js
// pages/Index/shopActive/coupon/index.js
const controllers = require('../../controllers/controllerIndex').controller
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponlist: [],
    pendimg: 'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/index/img/coupon_bg_05@2x.png',
    overimg: 'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/index/img/coupon_bg_07@2x.png',
    noimg: 'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/index/img/coupon_bg_09@2x.png',
    pageNumber: 1,
    pageSize: 10,
    isEnd: false,
  },
  //携带数据跳回上一页
  returnBychoose(e) {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];  //上一个页面
    let chooseCoupon=this.data.chooseCoupon;
    prevPage.setData({
      chooseCoupon:chooseCoupon
    })
    wx.navigateBack({
  
    })
  },
  //获取列表
  couponlist(){
 //获取优惠券列表
 controllers.getCouponList({
  shopId: wx.getStorageSync('shopId'),
  pageNumber: this.data.pageNumber,
  pageSize: this.data.pageSize,
}).then(res => {
  if (res.data.list.length != 0) {
    this.setData({
      couponlist: this.data.couponlist.concat(res.data.list)
    })
  }
  if(res.data.lastPage){
    this.setData({
      isEnd:true
    })
  }
  var all=this.data.couponlist
  all.forEach(function(item,index){
    let data=new Date().getTime();
    let start=new Date(item.startTime.replace(/-/g, '/') ).getTime()
    let end=new Date(item.endTime.replace(/-/g, '/') ).getTime()
    console.log(start+'ccc'+end);
    if(data<start){
      all[index].state=0
    }else if(data>start && data<end){
      all[index].state=1
    }else{
      all[index].state=2
    }
    console.log(all[index].state);
    
  })
  let list=[]
    for(let i=0;i<all.length;i++){
      if(all[i].state==1){
        all[i].checked=false
        list.push(all[i])
      }
    }
  this.setData({
    couponlist:list
  })
})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      couponlist: [],
      isEnd: false,
      pageNumber: 1,
    })
   this.couponlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
        pageNumber: this.data.pageNumber + 1,
      });
      this.couponlist()
    }
  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }

  radioChange(e) {
    console.log(e);
    let ids=e.detail.value.split("/")[0];
    let name=e.detail.value.split("/")[1];

    this.setData({
      'chooseCoupon.ids':ids,
      'chooseCoupon.name':name
    })
  }
})